(function($){

    $.fn.extend({
        banner:function(ops){
            var cont=this;
            ops.list=ops.list==false?false:true;
            ops.autoPlay=ops.autoPlay==false?false:true;
            ops.delayTime=ops.delayTime||2000;
            ops.moveTime=ops.moveTime||200;
            class Slide{
                constructor(ops){
                    this.img=ops.img;
                    this.left=ops.left;
                    this.right=ops.right;
                    this.list=ops.list;
                    this.auto=ops.autoPlay;
                    this.delayTime=ops.delayTime;
                    this.iNow=0;
                    this.iPrev=this.img.length-1;
                    if(this.list){
                        this.addList();
                    }
                    if(this.auto){
                        this.autoPlay();
                    }
                    this.addEvent();
                }
                addList(){
                    var str="";
                    for(var i=0;i<this.img.length;i++){
                        str+=`<li><span class="circle"></span></li>`;
                    }
                    $(`<ul id='dot'>${str}</ul>`).appendTo(cont);
                    this.changeDot();
                }

                addEvent(){
                    var that=this;
                    if(this.left!=undefined&&this.left.length>0&&this.right!=undefined&&this.right.length>0){
                        this.right.on("click",function(){
                            that.changeIndex(1);
                        })
                        this.left.on("click",function(){
                            that.changeIndex(-1);
                        })
                    }
                    cont.hover(()=>{
                        clearInterval(this.time);
    
                    },()=>{
                        this.autoPlay();
                    });
                    cont.on("click","#dot li",function(){
                        that.dotIndex=$(this).index();
                        if($(this).index()>that.iNow){
                            that.clickDot(1);
                            
                        }else{
                            that.clickDot(-1);
                        }
                        that.iNow=that.dotIndex;
                        that.changeDot();
                        return false;
                    })
                }
                clickDot(index){
                    this.img.eq(this.iNow).css("left",0).animate({"left":-this.img.eq(0).width()*index},this.moveTime);
                    this.img.eq(this.dotIndex).css("left",this.img.eq(0).width()*index).animate({left:0},this.moveTime);
                }
                autoPlay(){
                    this.time=setInterval(()=>{
                        this.changeIndex(1);
                    },this.delayTime);
                }
                changeIndex(i){
                    if(i==1){//右按钮
                        if(this.iNow==this.img.length-1){
                            this.iNow=0;
                            this.iPrev=this.img.length-1;
                        }
                        else{
                            this.iNow++;
                            this.iPrev=this.iNow-1;
                        }
                        this.slide(1);
    
                    }
                    else{
                        if(this.iNow==0){//到了第一张的时候
                            this.iNow=this.img.length-1;
                            this.iPrev=0;
                        }
                        else{
                            this.iNow--;
                            this.iPrev=this.iNow+1;
                        }
                        this.slide(-1);
                    }
                    this.changeDot();
                }
                slide(index){
                    this.img.eq(this.iPrev).css("left",0).stop().animate({left:-this.img.eq(0).width()*index},this.moveTime);
                    this.img.eq(this.iNow).css("left",this.img.eq(0).width()*index).stop().animate({left:0},this.moveTime);//使用stop是先停止上次的动画
                }
                changeDot(){
                    cont.find("#dot").children("li").eq(this.iNow).css({
                        background:"rgba(204,204,204,1)"
                    }).children(".circle").css("background","#ac926e").end().siblings().css({
                        background:"rgba(204,204,204,0)"
                    }).children(".circle").css("background","#fff");
                }
    
            }
            new Slide(ops);
        },
        
    })
})(jQuery);